package com.example.bbs.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.bbs.bean.User;
import com.example.bbs.util.HttpUtil;
import com.example.bbs.util.DisplayUtil;
import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;

import java.util.concurrent.CompletableFuture;

@SuppressLint("StaticFieldLeak")
public class Data {

    static final String[] PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    public static final String LOG_TAG = Data.class.getSimpleName();
    public static User myself = null;
    public static Context context;

    public static void init(Context context) { // 初始化数据与权限

        Data.context = context;
        DisplayUtil.init(context);
        requestPermissions(context);
        try {
            CompletableFuture.runAsync(() -> HttpUtil.init(context)).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        TypedArray actionbarSizeTypedArray = context.obtainStyledAttributes(new int[] {
                android.R.attr.actionBarSize
        });

        float h = actionbarSizeTypedArray.getDimension(0, 0);
        Log.d("test", "re: " + h);
        actionbarSizeTypedArray.recycle();
    }

    private static void requestPermissions(Context context) {
        Log.d(LOG_TAG, "request permissions");
        PermissionsUtil.requestPermission(context, new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permission) { }

            @Override
            public void permissionDenied(@NonNull String[] permission) { }
        }, PERMISSIONS);
    }
}
